package tech.lq0.dreamaticvoyage.compat.jei;

import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.drawable.IDrawableAnimated;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;
import tech.lq0.dreamaticvoyage.recipe.FukamizuCompressingRecipe;

import javax.annotation.ParametersAreNonnullByDefault;
import java.text.DecimalFormat;

public class FukamizuCompressingCategory implements IRecipeCategory<FukamizuCompressingRecipe> {

    public static final ResourceLocation TEXTURE = Utils.loc("textures/gui/fukamizu_punch.png");
    public static final RecipeType<FukamizuCompressingRecipe> TYPE = new RecipeType<>(Utils.loc("fukamizu_compressing"),
            FukamizuCompressingRecipe.class);

    private final IDrawable background;
    private final IDrawable icon;
    private final IDrawableAnimated piston;

    public FukamizuCompressingCategory(IGuiHelper helper) {
        this.background = helper.drawableBuilder(TEXTURE, 46, 24, 89, 51)
                .setTextureSize(256, 256)
                .build();

        this.icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, new ItemStack(ItemRegistry.FUKAMIZU_COMPRESSOR.get()));
        this.piston = helper.drawableBuilder(TEXTURE, 177, 0, 23, 12)
                .setTextureSize(256, 256)
                .buildAnimated(100, IDrawableAnimated.StartDirection.LEFT, false);
    }

    @Override
    @ParametersAreNonnullByDefault
    public void draw(FukamizuCompressingRecipe recipe, IRecipeSlotsView recipeSlotsView, GuiGraphics guiGraphics, double mouseX, double mouseY) {
        piston.draw(guiGraphics, 29, 28);

        var component = Component.translatable("gui.jei.category.smelting.time.seconds", new DecimalFormat("##.#").format(recipe.getTick() / 20.0f));
        guiGraphics.drawString(Minecraft.getInstance().font,
                component, 87 - Minecraft.getInstance().font.width(component), 3, 5592405, false);
    }

    @Override
    public @NotNull RecipeType<FukamizuCompressingRecipe> getRecipeType() {
        return TYPE;
    }

    @Override
    public @NotNull Component getTitle() {
        return Component.translatable("gui.jei.category.dreamaticvoyage.fukamizu_compressing");
    }

    @Override
    public @NotNull IDrawable getBackground() {
        return background;
    }

    @Override
    public @NotNull IDrawable getIcon() {
        return icon;
    }

    @Override
    @ParametersAreNonnullByDefault
    public void setRecipe(IRecipeLayoutBuilder builder, FukamizuCompressingRecipe recipe, IFocusGroup group) {
        builder.addSlot(RecipeIngredientRole.INPUT, 5, 26).addIngredients(recipe.getIngredients().get(0));
        builder.addSlot(RecipeIngredientRole.CATALYST, 33, 6).addIngredients(recipe.getMold());
        builder.addSlot(RecipeIngredientRole.OUTPUT, 64, 26).addItemStack(recipe.getResultItem(null));
    }
}
